package cn.itrigger.dynamic.sql.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author admin
 * @date 2018/9/21
 **/
public class FragmentAndParameters {
    private String fragment;
    private Map<String, Object> parameters;

    protected FragmentAndParameters(Builder builder) {
        this.fragment = Objects.requireNonNull(builder.fragment);
        this.parameters = Objects.requireNonNull(builder.parameters);
    }

    public String getFragment() {
        return fragment;
    }

    public Map<String, Object> getParameters() {
        return parameters;
    }

    public FragmentAndParameters prependFragment(String s) {
        return FragmentAndParameters.withFragment(s + " " + fragment)
                .withParameter(parameters)
                .build();
    }

    public static Builder withFragment(String fragment) {
        return new Builder().withFragment(fragment);
    }

    public static class Builder {
        private String fragment;
        private Map<String, Object> parameters = new HashMap<>();

        public Builder withFragment(String fragment) {
            this.fragment = fragment;
            return this;
        }

        public Builder withParameter(Map<String, Object> parameters) {
            this.parameters.putAll(parameters);
            return this;
        }

        public Builder withParameter(String key, Object value) {
            parameters.put(key, value);
            return this;
        }

        public FragmentAndParameters build() {
            return new FragmentAndParameters(this);
        }
    }
}
